<template>
  <div>
    <div class="Searchbox">
      <el-card class="box-card" shadow="never">
        <el-form ref="form" :model="form" label-width="80px">
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item label="问卷名称">
                <el-input v-model="seleceDeptToPage.name"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="4">
              <el-form-item label="问卷类型">
                <el-select v-model="seleceDeptToPage.questionTypeId" placeholder="请选择">
                  <el-option label="全部"></el-option>
                  <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="2">
              <el-button type="primary" @click="getDeptToPages(1)" icon="el-icon-search">搜索</el-button>
            </el-col>

            <el-col :span="4">
              <el-button type="primary" icon="el-icon-circle-plus-outline" @click="dialogFormVisible=true, flag=1,disabled= false,AddDeptData={},title='添加问卷'">添加</el-button>
            </el-col>
          </el-row>
          <!-- 
          <el-row :gutter="4">
            <el-button type="primary" icon="el-icon-circle-plus-outline" @click="dialogFormVisible=true,flag=1,disabled= false,AddDeptData={},title='添加职能部门'">添加</el-button>
          </el-row>-->
        </el-form>
      </el-card>
      <br />
      <el-card class="box-card" shadow="never">
        <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
          <el-table-column prop="questionName" label="问卷名称" width="200"></el-table-column>
          <el-table-column prop="year" label="年份" width="100"></el-table-column>
          <el-table-column prop="evaluationTypeName" label="评价类型" width="150"></el-table-column>
          <el-table-column prop="questionTypeName" label="问卷类型" width="150"></el-table-column>
          <el-table-column prop="questionTypeName" label="是否发布" width="100">
            <template slot-scope="scope">
              <span v-html="scope.row.isRelease=='N'?'未发布':'已发布'"></span>
            </template>
          </el-table-column>
          <el-table-column prop="startTime" label="开始时间" width="150">
            <template slot-scope="scope">{{scope.row.startTime | time}}</template>
          </el-table-column>
          <el-table-column prop="endTime" label="结束时间" width="150">
            <template slot-scope="scope">{{scope.row.endTime | time}}</template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" >
            <template slot-scope="scope">
              <el-button v-if="scope.row.isRelease!=='Y'" @click="fabuid(scope.row.id)" type="text" size="small">发布</el-button>
              <el-button v-if="scope.row.isRelease!=='N'" @click="handleClick(scope.row),disabled = true" type="text" size="small">答题状态</el-button>
              <el-button type="text" size="small" @click="eidtData(scope.row),title='编辑问卷'">编辑</el-button>


              <el-button @click="doDeleteDeptBox(scope.row.id)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagebox">
          <el-pagination background layout="prev, pager, next" :page-size="seleceDeptToPage.pageSize" :total="dataCount" @current-change="handleCurrentChange"></el-pagination>
        </div>
      </el-card>
    </div>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%">

      <el-form :model="form" label-position="right" :disabled="disabled">
        <el-form-item label="问卷名称" label-width="100px">
          <el-input v-model="AddDeptData.questionName" style="width:200px;"></el-input>
        </el-form-item>

        <el-form-item label="年份" label-width="100px">
          <el-date-picker v-model="AddDeptData.year" style="width:200px;" value-format="yyyy" type="year" placeholder="选择年"></el-date-picker>
        </el-form-item>

        <el-form-item label="评价类型" label-width="100px">
          <el-select v-model="AddDeptData.evaluationTypeId" style="width:200px;" placeholder="请选择">
            <el-option v-for="item in evaluateTypeData" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="问卷类型" label-width="100px">
          <el-select :disabled="AddDeptData.isRelease=='Y'" v-model="AddDeptData.questionTypeId"  style="width:200px;" placeholder="请选择">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="开始时间" label-width="100px">
          <el-date-picker v-model="AddDeptData.startTime" style="width:200px;"s value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" label-width="100px">
          <el-date-picker v-model="AddDeptData.endTime" style="width:200px;"s value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>

        <el-form-item label="小组配置" label-width="100px" v-if=" AddDeptData.questionTypeId =='cf6d84eb727b48519f31d5362abf7153'">
          <el-select v-model="AddDeptData.groupIds" placeholder="请选择">
            <el-option v-for="item in groupIdsData" :key="item.id" :label="item.grouName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" label-width="100px">
          <el-input type="textarea"  v-model="AddDeptData.remarks" style="width:200px;"></el-input>
        </el-form-item>

        <el-form-item label="问卷详情" v-if="disabled" label-width="100px">
          <div class="xiangqingbox">
            <p class="xiangqing" v-for="(item , index) in AddDeptData.listDetail">{{ index+1 }}、{{item.optionsName}}</p>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false,AddDeptData = {}" v-if="!disabled">取 消</el-button>
        <el-button type="primary" v-if="flag==2" @click="dialogFormVisible = false">确 定</el-button>
        <el-button type="primary" v-if="flag==1" @click="getAddDept()">确 定</el-button>
        <el-button type="primary" v-if="flag==3" @click="doupdateDeptMessage()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="AddDeptDataShow" width="500px">
 
    <div v-if="questionTypeId=='cf6d84eb727b48519f31d5362abf7153'">
      <el-table :data="AddDeptDataTabel" style="width: 100%">

           <el-table-column prop="deptName" label="城市公司" width="130"></el-table-column>
          <el-table-column prop="isCommit" label="是否提交"  >
                <template slot-scope="scope">
                  <span v-html="scope.row.isCommit=='Y'?'是':'否'"></span>
                </template>
            </el-table-column>

        </el-table>
      </div>
      <div v-if="questionTypeId=='54e3da78fb334841bb5cce4a6b927c7d'">
        <el-table :data="AddDeptDataTabel" style="width: 100%">


        <el-table-column prop="deptName" label="职能部门" width="130"></el-table-column>
       <el-table-column prop="isCommit" label="是否提交"  >
            <template slot-scope="scope">
              <span v-html="scope.row.isCommit=='Y'?'是':'否'"></span>
            </template>
        </el-table-column>

        </el-table>
        <!--div class="pagebox">
          <el-pagination background layout="prev, pager, next" :page-size="zhinengToPage.pageSize" :total="zhinengToPageCount" @current-change="handle"></el-pagination>
        </div-->
      </div>
      <div v-if="questionTypeId=='7c680ab28aa1454ebbb15c9ae1965876'">
      <el-table :data="AddDeptDataTabel" style="width: 100%">
       <el-table-column prop="deptName" label="城市公司" width="130"></el-table-column>
       <el-table-column prop="isCommit" label="是否提交"  >
            <template slot-scope="scope">
              <span v-html="scope.row.isCommit=='Y'?'是':'否'"></span>
            </template>
        </el-table-column>

          <!--el-table-column prop="coverDeptName" label="职能部门" width="180"></el-table-column>
          <el-table-column prop="deptName" label="城市公司" width="130"></el-table-column>
          <el-table-column prop="dataType" label="评价模块" width="100"></el-table-column>
          <el-table-column prop="name" label="评价人" width="240"></el-table-column>
          <el-table-column prop="optionsName" label="评价问题" width="240"></el-table-column>
          <el-table-column prop="optionsResult" label="得分" width="80"></el-table-column>
          <el-table-column prop="reason" label="原因示例"  ></el-table-column-->
        </el-table>
        <!--div class="pagebox">
          <el-pagination background layout="prev, pager, next" :page-size="zhinengToPage.pageSize" :total="zhinengToPageCount" @current-change="handle"></el-pagination>
        </div-->
      </div>
       

      <span slot="footer" class="dialog-footer">
    <el-button @click="AddDeptDataShow = false">取 消</el-button>
    <!-- <el-button type="primary" @click="doDataToExcelMx(row.id,row.questionName)">导出评价明细</el-button> -->
    <el-button type="primary" @click="doDataToExcel(row.id,row.questionName)">导出评价结果</el-button>
  </span>

    </el-dialog>
  </div>
</template>


<script>
import {
  addSysQuestion,
  updateSysQuestionMessage,
  getSysQuestionToPage,
  getAllSysQuestionToList,
  DeleteSysQuestion,
  getSysQuestionById,
  releaseQuestion,
  getDictionariesByType,
  DataToExcel,
  DataToExcelMx,
  getAllGroupConfigToList,
  getQuestionHistoryById,
} from "@/api/api";

export default {
  data() {
    return {
      loading:false,
      title: "",
      flag: 0,
      disabled: true,
      dialogFormVisible: false,
      AddDeptDataShow: false,
      seleceDeptToPage: {
        questionTypeId: "",
        name: "",
        pageIndex: 1,
        pageSize: 10,
      },
      form: {},
      AddDeptData: {},
      tableData: [],
      dataCount: 0,
      options: [],
      evaluateTypeData: [],
      groupIdsData: [],
      AddDeptDataTabel: [],
      questionTypeIddata: "",
      row:{},
      questionTypeId: "",
      
      zhinengToPage: {
        pageIndex: 1,
        pageSize: 10
      },
      zhinengToPageCount:0
    };
  },

  mounted() {
    let that = this;
    that.getDeptToPages(1);
    that.getDictionariesByTypes("questionType");
    that.getDictionariesByTypes("evaluateType");
    that.getAllGroupConfigToLists();
  },

  filters: {
    time(value) {
      let time = value.replace(/T/g, "");
      time = time.slice(0, time.length - 8);
      return time;
    },
  },

  methods: {
    async fabuid(id) {
      let that = this;
      that.loading = true
      let params = {
        questionId: id,
      };
      let res = await releaseQuestion(params);
      if (res.data.Code == 200) {
        that.getDeptToPages(that.seleceDeptToPage.pageIndex);
        that.loading = false
        this.$message.success(res.data.Msg);
      } else if (res.data.Code == 400) {
        that.loading = false
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        that.loading = false
        this.$router.push({ path: "/login" });
      }
    },

    async getAllGroupConfigToLists() {
      let that = this;
      let params = {};
      let res = await getAllGroupConfigToList(params);
      if (res.data.Code == 200) {
        that.groupIdsData = res.data.Data;
      } else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },
    async doDataToExcelMx(id,name){
      let that = this;
      let params = {
        questionId: id,
      };
      let res = await DataToExcelMx(params);
      let blob = new Blob([res.data]); // { type: "application/vnd.ms-excel" }
      let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
      // 创建url之后可以模拟对此文件对象的一系列操作，例如：预览、下载
      let a = document.createElement("a");
      a.href = url;
      a.download = name+"明细.xlsx";
      a.click();
      // 释放这个临时的对象url
      window.URL.revokeObjectURL(url);
    },

    async doDataToExcel(id,name) {
      let that = this;
       
      let params = {
        id: id,
      };
      let res = await DataToExcel(params);
      let blob = new Blob([res.data]); // { type: "application/vnd.ms-excel" }
      let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
      // 创建url之后可以模拟对此文件对象的一系列操作，例如：预览、下载
      let a = document.createElement("a");
      a.href = url;
      a.download = name+".xlsx";
      a.click();
      // 释放这个临时的对象url
      window.URL.revokeObjectURL(url);
    },

    async getDictionariesByTypes(flag) {
      let that = this;
      let params = {
        type: flag,
      };
      let res = await getDictionariesByType(params);
      if (res.data.Code == 200) {
        if (flag == "questionType") {
          that.options = res.data.Data;
        } else if (flag == "evaluateType") {
          that.evaluateTypeData = res.data.Data;
        }
      } else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    doDeleteDeptBox(id) {
      this.$confirm("若删除问卷则问卷历史也会同步删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.doDeleteDept(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    async doDeleteDept(id) {
      let that = this;
      let params = {
        id: id,
      };
      let res = await DeleteSysQuestion(params);
      if (res.data.Code == 200) {
        this.$message.success(res.data.Msg);
        that.getDeptToPages(that.seleceDeptToPage.pageIndex);
      } else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    eidtData(row) {
      let that = this;
      that.dialogFormVisible = true;
      that.AddDeptData = row;
      that.AddDeptData.groupIds = that.AddDeptData.groupIds;
      that.flag = 3;
      that.disabled = false;
    },

    async doupdateDeptMessage() {
      let that = this;
      let params = that.AddDeptData;

      if (
        that.AddDeptData.questionTypeId !== "cf6d84eb727b48519f31d5362abf7153"
      ) {
        that.AddDeptData.groupIds = "";
      }

      that.AddDeptData.groupIds = that.AddDeptData.groupIds.toString();

      let res = await updateSysQuestionMessage(params);
      if (res.data.Code == 200) {
        this.$message.success(res.data.Msg);
        that.dialogFormVisible = !that.dialogFormVisible;
        that.getDeptToPages(that.seleceDeptToPage.pageIndex);
      } else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    async handleClick(row) {
      let that = this;
      that.row = row
      that.questionTypeIddata = row.questionTypeId;
      that.questionTypeId = row.questionTypeId;
      that.flag = 2;
      that.disabled = false;
      that.title = row.questionName;
      that.AddDeptDataShow = !that.AddDeptDataShow;

      that.getQuestionHistorydata()

    },
    async getQuestionHistorydata(){
      let that = this
      let params = that.zhinengToPage
      params.questionId = that.row.id;
      let res = await getQuestionHistoryById(params);
      if (res.data.Code == 200) {
        that.AddDeptDataTabel = res.data.Data;
        that.zhinengToPageCount = res.data.dataCount;
      } else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    async getAddDept() {
      let that = this;

      if (
        that.AddDeptData.questionName == undefined ||
        that.AddDeptData.questionName == ""
      ) {
        that.$message.warning("问卷名称不能为空！");
        return;
      }

      if (that.AddDeptData.year == undefined || that.AddDeptData.year == "") {
        that.$message.warning("年份不能为空！");
        return;
      }

      if (
        that.AddDeptData.evaluationTypeId == undefined ||
        that.AddDeptData.evaluationTypeId == ""
      ) {
        that.$message.warning("评价类型不能为空！");
        return;
      }
      if (
        that.AddDeptData.questionTypeId == undefined ||
        that.AddDeptData.questionTypeId == ""
      ) {
        that.$message.warning("问卷类型不能为空！");
        return;
      }
      if (
        that.AddDeptData.startTime == undefined ||
        that.AddDeptData.startTime == ""
      ) {
        that.$message.warning("开始时间不能为空！");
        return;
      }

      if (
        that.AddDeptData.endTime == undefined ||
        that.AddDeptData.endTime == ""
      ) {
        that.$message.warning("结束时间不能为空！");
        return;
      }

      let params = that.AddDeptData;
      if (
        that.AddDeptData.questionTypeId !== "cf6d84eb727b48519f31d5362abf7153"
      ) {
        that.AddDeptData.groupIds = "";
      }
      that.AddDeptData.groupIds = that.AddDeptData.groupIds.toString();
      let res = await addSysQuestion(params);
      if (res.data.Code == 200) {
        this.$message.success(res.data.Msg);
        that.dialogFormVisible = false;
        that.getDeptToPages(that.seleceDeptToPage.pageIndex);
      } else if (res.data.Code == 400) {
        that.dialogFormVisible = false;
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        that.dialogFormVisible = false;
        this.$router.push({ path: "/login" });
      }
    },

    handleCurrentChange(val) {
      let that = this;
      // that.seleceDeptToPage.pageIndex = val;
      that.getDeptToPages(val);
    },

    handle(val) {
      let that = this;
      that.zhinengToPage.pageIndex = val;
      that.getQuestionHistorydata()
    },

    async getDeptToPages(index) {
      let that = this;
      that.seleceDeptToPage.pageIndex = index;
      let params = that.seleceDeptToPage;
      let res = await getSysQuestionToPage(params);
      if (res.data.Code == 200) {
        that.tableData = res.data.Data;
        that.dataCount = res.data.dataCount;
      } else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },
  },
};
</script>

<style scoped>
.Searchbox {
}

.xiangqingbox {
  padding: 1px 14px;
  background: #f5f7fa;
  border: 1px #eee solid;
  border-radius: 5px;
  color: #666;
}
.xiangqing {
  line-height: 10px;
}
</style>